package hbk;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.Scanner;
import javax.swing.*;


public class DbConnector {

    // Variabler
    private String bruger, password, databaseUrl;
    private Connection conn;
    private Statement stmt;
    private ResultSet result;
    private Scanner fileScan;
    private String host;
    private String port;
    private String dbNavn;


//    public DbConnector(String bruger, String password, String databaseUrl) {
//        this.bruger = bruger;
//        this.password = password;
//        this.databaseUrl = databaseUrl;
//        conn = null;
//        stmt = null;
//        result = null;
//    }
    public DbConnector()    {
        try {

            fileScan = new Scanner(new FileInputStream("Database.ini"));
            host = fileScan.nextLine();
            port = fileScan.nextLine();
            dbNavn = fileScan.nextLine();
            bruger = fileScan.nextLine();
            password = fileScan.nextLine();

            databaseUrl = host + ":" + port + "/" + dbNavn;

         
        } catch (FileNotFoundException e) {
            System.out.println("Filen kunne ikke åbnes " + e.getLocalizedMessage());
            System.exit(0);
        }
    }

    public ResultSet executeQuery(String query) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(databaseUrl, bruger, password);
            stmt = conn.createStatement();
            result = stmt.executeQuery(query);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "SQL fejl: " + e.getLocalizedMessage());
        } catch (InstantiationException e) {
            JOptionPane.showMessageDialog(null, "Fejl ved instantiering: " + e.getLocalizedMessage());
        } catch (IllegalAccessException e) {
            JOptionPane.showMessageDialog(null, "Ulovlig tilgang: " + e.getLocalizedMessage());
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Klassen blev ikke fundet: " + e.getLocalizedMessage());
        }
        return result;
    }

    public void executeUpdate(String query) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(databaseUrl, bruger, password);
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "SQL fejl: " + e.getLocalizedMessage());
        } catch (InstantiationException e) {
            JOptionPane.showMessageDialog(null, "Fejl ved instantiering: " + e.getLocalizedMessage());
        } catch (IllegalAccessException e) {
            JOptionPane.showMessageDialog(null, "Ulovlig tilgang: " + e.getLocalizedMessage());
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Klassen blev ikke fundet: " + e.getLocalizedMessage());
        }
    }

    public void close() {
        try {
            if (result != null) {
                result.close();
            }

            if (stmt != null) {
                stmt.close();
            }

            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "SQL fejl: " + e.getLocalizedMessage());
        }
    }
}
